package com.useeinfo.oa.open.api.inspection;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.Configuration;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.PositionBiz;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Position;
import com.useeinfo.oa.modules.inspection.biz.InspectionOrderBiz;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/mobile/inspectionUser")
public class InspectionOrderUserAPI {

    @Autowired
    private InspectionOrderBiz inspectionOrderBiz;
    @Autowired
    private SystemUserBiz systemUserBiz;
    @Autowired
    private PositionBiz positionBiz;

    //指定人员json数据  指派人员
    @RequestMapping("/getUser")
    public ResultDto getUserByArea(HttpServletRequest request,
                                   String pageIndex, String pageSize, String userName, String userIds) {
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if (null == enterprise) {
            return ResultUtil.error("企业信息有误");
        }
      return inspectionOrderBiz.getUser(pageIndex,pageSize,enterprise,userName,userIds);
    }


    //指定人员json数据  指派人员
    @RequestMapping("/getPosition")
    public ResultDto getPosition(HttpServletRequest request,
                                 String pageIndex, String pageSize,String positionIds) {
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if (null == enterprise) {
            return ResultUtil.error("企业信息有误");
        }
        return inspectionOrderBiz.getPosition(pageIndex,pageSize,enterprise,positionIds);

    }


    /**
     * 保存指定人
     *
     * @param assignIds
     * @param inspectionOrderId
     * @return
     */
    @RequestMapping("/saveAssignList")
    @ResponseBody
    public ResultDto saveAssignList(String assignIds, String inspectionOrderId, HttpServletRequest request) {
        return inspectionOrderBiz.assignUsersApp(assignIds, inspectionOrderId, request);
    }

    /**
     * 保存指定岗位
     *
     * @param assignPositionIds
     * @param inspectionOrderId
     * @return
     */
    @RequestMapping("/saveAssignPositionList")
    @ResponseBody
    public ResultDto saveAssignPositionList(String assignPositionIds, String inspectionOrderId, HttpServletRequest request) {
        return inspectionOrderBiz.assignPositionsApp(assignPositionIds, inspectionOrderId, request);
    }

    /**
     * 工单转单
     *
     * @param inspectionOrderIds 工单ID数组
     * @param userId   工单被接单人的ID
     * @return
     */
    @RequestMapping("/setReceiver")
    public ResultDto setReceiver(String inspectionOrderIds, String userId) {
        try {
            return inspectionOrderBiz.setReceiver(inspectionOrderIds, userId);
        } catch (Exception e) {
            return ResultUtil.error(e.getMessage());
        }

    }
}
